package com.iwhalecloud.citybrain.flow.platform.manager.application.config;

import com.aliyun.gts.bpaas.middleware.mq.api.MessageProducer;
import com.aliyun.gts.bpaas.middleware.mq.autoconfigure.OnsAutoConfigure;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CallbackThirdPlatformAfterTaskCreatedService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.MqProducer;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task.SendMqMessagePostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReModelCustomConfigRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author lilh
 * @date 2021/4/25 09:21
 */
@Configuration
//@ConditionalOnBean(MessageProducer.class)
@Conditional({OnsAutoConfigure.OnsCacheCondition.class})
public class MqConfig {

    @Value("${flow.config.task.mq.topic:FLOW_PUB_TOPIC}")
    private String topic;

    @Bean
    public MqProducer mqProducer(MessageProducer messageProducer) {
        return new MqProducer(messageProducer, topic);
    }

    @Bean
    @Order(50)
    public SendMqMessagePostProcessor sendMqMessagePostProcessor(CallbackThirdPlatformAfterTaskCreatedService taskResolver,
                                                               MessageProducer messageProducer,
                                                               ThreadPoolTaskExecutor taskExecutor,
                                                               FlowReModelCustomConfigRepository modelCustomConfigRepository) {
        return new SendMqMessagePostProcessor(taskResolver, mqProducer(messageProducer), taskExecutor, modelCustomConfigRepository);
    }

}
